package yxy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XingZhiXingZhuangTongJi {

	private static String getCellvalue(Row row, int i) {
		Cell cell = row.getCell(i);
		if (cell == null) {
			return "";
		} else {
			if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
				return String.valueOf((int) cell.getNumericCellValue());
			}
			try {
				return cell.getStringCellValue().trim();
			} catch (Exception e) {
				e.printStackTrace();
				return "";
			}
		}
	}
	
	public static Map<String, List<Integer>> getMap(int index, List<List<String>> datas) {
		Map<String, List<Integer>> map = new LinkedHashMap<>();
		for (List<String> data : datas) {
			String key = data.get(0);
			List<Integer> list = map.get(key);
			
			//第一次扫描
			if (list == null) 
			{
				list = new ArrayList<>();
				map.put(key, list);
				
				//有数据
				if (StringUtils.isNotBlank(data.get(index))) 
				{
					list.add(1);
					if (data.get(index).equals("2")) {
						list.add(1);
					} else {
						list.add(0);
					}
				} 
				//没数据
				else 
				{
					list.add(0);
					list.add(0);
				}
			} 
			
			else 
			{
				//不为空才会记录总数
				if (StringUtils.isNotBlank(data.get(index))) 
				{
					//第一个是总数
					list.set(0, list.get(0) + 1);
					if (data.get(index).equals("2")) {
						list.set(1, list.get(1) + 1);
					}
				}
			}
		}
		
		return map;
	}

	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		File f = new File("C:\\Users\\Administrator\\Desktop\\excel处理\\形质性状.xlsx");
		XSSFWorkbook xwb = new XSSFWorkbook(f.getAbsolutePath());

		List<List<String>> datas = new ArrayList<>();
		Sheet sheet = xwb.getSheetAt(0);
		for (int i = 2; i < sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			List<String> list = new ArrayList<>();
			for (int j = 0; j < 6; j++) {
				list.add(StringUtils.deleteWhitespace(getCellvalue(row, j)));
			}
			datas.add(list);
		}
		
		
		for (List<String> data : datas) {
			String block = data.get(0);
			String family = data.get(1);
			data.remove(0);
			data.remove(0);
			data.add(0, block + ";" + family);
		}

		Map<String, List<Integer>> ztcx = getMap(1, datas);
		Map<String, List<Integer>> csz = getMap(2, datas);
		Map<String, List<Integer>> dsg = getMap(3, datas);
		Map<String, List<Integer>> cz = getMap(4, datas);
		
		System.out.println(ztcx);
		System.out.println(csz);
		System.out.println(dsg);
		System.out.println(cz);
		
		File f2 = new File("C:\\Users\\Administrator\\Desktop\\excel处理\\形质性状保存结果.xlsx");
		XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(f2));
		Sheet sheet2 = workbook.getSheetAt(0);
		int i = 1;	
		for (String key : ztcx.keySet()) {
			List<Integer> l1 = ztcx.get(key);
			List<Integer> l2 = csz.get(key);
			List<Integer> l3 = dsg.get(key);
			List<Integer> l4 = cz.get(key);
			
			Row row = sheet2.createRow(i);
			String[] keys = key.split(";");
			for (int j = 0; j < keys.length; j++) {
				Cell cell = row.createCell(j);
				cell.setCellValue(keys[j]);
			}
			Cell cell2 = row.createCell(2);
			cell2.setCellValue(l1.get(1) + "/" + l1.get(0));
			
			Cell cell3 = row.createCell(3);
			cell3.setCellValue(l2.get(1) + "/" + l2.get(0));
			
			Cell cell4 = row.createCell(4);
			cell4.setCellValue(l3.get(1) + "/" + l3.get(0));
			
			Cell cell5 = row.createCell(5);
			cell5.setCellValue(l4.get(1) + "/" + l4.get(0));
			
			
			i++;
		}
		FileOutputStream fileOutputStream = new FileOutputStream(f2);
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("cehgg ");
        

	}

}
